﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Microsoft.WindowsAzure.MobileServices;
using WCFServiceModel;
using WCFServiceModel.Model;
using System.Threading.Tasks;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace WCFServiceModel
{
    public sealed partial class AnalyticsControl : UserControl
    {
        private Guid? _id = null;

        /// <summary>
        /// singletTon object
        /// </summary>

        public Guid? Id
        {
            get
            {
                return _id;
            }
            set
            {

                if (_id == null)
                    _id = value;
            }
        }
        /// <summary>
        /// Initialize control and  construct a session 
        /// </summary>
        /// 
        private User user = null;
        private Session session= null;
        private WCFServiceModel.Model.Application app= null;

        private string _appName;

        public string AppName
        {
            get { return _appName; }
            set { _appName = value; }
        }
        private MobileServiceClient wcfClient = ServiceManager.getCurrentContext();
        public AnalyticsControl()
        {
            this.InitializeComponent();
            if (user == null)
            {
                user = new User(15, "aSd@live.com", 33, 33);
            }
            if (app == null)
            {
                app = new WCFServiceModel.Model.Application(AppName, user);
            }
            if (session == null)
            {
                session = new Session(user.Id, app.Id);
            }
            session = new Session();

        }

        private void Grid_DoubleTapped_1(object sender, DoubleTappedRoutedEventArgs e)
        {
            
        }

        private void Analytics_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {

        }

        private void Analytics_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        private void Analytics_Holding(object sender, HoldingRoutedEventArgs e)
        {

        }

        private void Analytics_Loaded(object sender, RoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.Loaded);
        }

        private void Analytics_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.ManipulationPointer);
        }

        private void Analytics_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.ManipulationPointer);
        }

        private void Analytics_PointerEntered(object sender, PointerRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.ManipulationPointer,e.GetCurrentPoint(this).Position.X,e.GetCurrentPoint(this).Position.Y);
        }

        private void Analytics_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.ManipulationPointer, e.GetCurrentPoint(this).Position.X, e.GetCurrentPoint(this).Position.Y);
        }

        private void Analytics_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.ManipulationPointer, e.GetCurrentPoint(this).Position.X, e.GetCurrentPoint(this).Position.Y);
        }

        private void Analytics_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.ManipulationPointer, e.GetCurrentPoint(this).Position.X, e.GetCurrentPoint(this).Position.Y);
        }

        private void Analytics_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, parentObject.ToString(), e.GetType().ToString(), Model.Enums.ActionType.ManipulationPointer, e.GetCurrentPoint(this).Position.X, e.GetCurrentPoint(this).Position.Y);
        }

        private void Analytics_RightTapped(object sender, RightTappedRoutedEventArgs e)
        {

        }

        private void Analytics_Tapped(object sender, TappedRoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, "", e.GetType().ToString(), Model.Enums.ActionType.Tap);
        }

        private void Analytics_LostFocus(object sender, RoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, "", e.GetType().ToString(), Model.Enums.ActionType.LostFocus);
        }

        private void Analytics_Unloaded(object sender, RoutedEventArgs e)
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(this);
            new WCFServiceModel.Model.Action(session.Id, "", e.GetType().ToString(), Model.Enums.ActionType.Unload);
        }
    }
}
